package com.yanqu.road.server;

import com.yanqu.road.NettyMgr;
import com.yanqu.road.dao.db.DBTableInfoMgr;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.manager.CrossDataAnalyticsMgr;
import com.yanqu.road.server.manager.activity.ActivityDisperseGoldMgr;
import com.yanqu.road.server.manager.activity.mammonhall.CrossMammonHallMgr;
import com.yanqu.road.server.manager.banquet.*;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortCacheMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortRankMgr;
import com.yanqu.road.server.manager.config.*;
import com.yanqu.road.server.manager.curio.CrossCurioMgr;
import com.yanqu.road.server.manager.grandchild.GrandChildExamConfigMgr;
import com.yanqu.road.server.manager.grandchild.GrandChildMgr;
import com.yanqu.road.server.manager.grandchild.GrandChildRankMgr;
import com.yanqu.road.server.manager.group.GroupChatMgr;
import com.yanqu.road.server.manager.group.SystemGroupChatMgr;
import com.yanqu.road.server.manager.marriage.CrossGroupMarriageMgr;
import com.yanqu.road.server.manager.marriage.CrossMarriageGroupChatMgr;
import com.yanqu.road.server.manager.match.CrossMatchConfigMgr;
import com.yanqu.road.server.manager.match.CrossMatchMgr;
import com.yanqu.road.server.manager.mogulbirthday.MogulBirthdayMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketConfigMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketCrossGroupMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manager.wingroom.CrossWingRoomMgr;
import com.yanqu.road.server.manager.workshop.WorkShopMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsConfigMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsGroupChatMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsRankMgr;
import com.yanqu.road.server.manger.*;
import com.yanqu.road.server.timer.GeneralAgentTimerMgr;

import java.util.ArrayList;
import java.util.List;

public final class CrossCenterServer extends CrossBaseServer {

    private static CrossCenterServer crossCenterServer;

    public static CrossCenterServer getInstance() {
        if (null == crossCenterServer) {
            crossCenterServer = new CrossCenterServer();
            if(null == crossBaseServer) {
                crossBaseServer = crossCenterServer;
            }else {
                logger.error("CrossCenterServer getInstance baseServer exist");
            }
        }
        return crossCenterServer;
    }

    @Override
    public boolean stop() {
        try {
            super.stop();
            //保存mgr数据
            for (TempMgr tempMgr : CrossCenterServer.getInstance().getMgrList()) {
                try {
                    tempMgr.save();
                }catch (Exception e){
                    logger.error("mgr save error:", e);
                }
            }
        } catch (Exception e) {
            logger.error("CrossCenterServer close error", e);
            return false;
        }

        return true;
    }

    public void newStop(){
        try {
            Thread.sleep(5000);
            // 关闭netty 避免有新连接
            NettyMgr.newStop();
            // 关闭定时器
            TempServer.getTimerExecutorService().shutdown();
            Thread.sleep(1000);
            // 停止
            for (TempMgr tempMgr : getInstance().getMgrList()){
                tempMgr.stop();
            }
            // 保存
            for (TempMgr tempMgr : getInstance().getMgrList()){
                try {
                    tempMgr.save();
                }catch (Exception e){
                    logger.error("mgr save error:", e);
                }
            }

            logger.info("CrossCenterServer 服务器已关闭");
            TempServer.deleteFile("./crossServer.pid");
            super.stopWithLogger();
            System.out.println("CrossCenterServer 服务器已关闭");
        }catch (Exception e) {
            logger.error("CrossBattleServer stop error", e);
        }
    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        if (args.length <= 0) {
            System.err.println("check set config path");
            return;
        }

        cfgPath = args[0];

        CrossCenterServer crossCenterServer = CrossCenterServer.getInstance();
        crossCenterServer.setServerType(Integer.valueOf(args[1]));
        if (!crossCenterServer.start(crossCenterServer.getServerType(), CrossCenterServer.class.getName())) {
            System.err.println("CrossCenterServer start fail!");
            System.exit(1);
            return;
        }


        DBTableInfoMgr dbTableInfoMgr = new DBTableInfoMgr();
        dbTableInfoMgr.init(DbNameStrategy.getCross2Db(), "com.yanqu.road.entity.log", true);
        crossCenterServer.setDbTableInfoMgr(dbTableInfoMgr);

        GeneralAgentTimerMgr.initGeneralTimers(crossCenterServer.getServerType());
        System.err.println("CrossCenterServer start success");
        logger.info("CrossCenterServer start success,cost time:" + (System.currentTimeMillis() - time));
        TempServer.tempServerLogger.info("notify process start success!");

        //注册异常关闭处理过程
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            logger.info("CrossCenterServer 关闭服务器");
            CrossCenterServer.getInstance().newStop();
        }));
    }

	@Override
	public List<TempMgr> getMgrList() {
		List<TempMgr> mgrList = new ArrayList<>();
		//配置放前面
        mgrList.add(new CrossCheckConfigVersionMgr());
		mgrList.add(new ConfigMgr());
        mgrList.add(new ChannelConfigMgr(sType));
		mgrList.add(new ServerListMgr());
		mgrList.add(new ServerGroupMgr());
		mgrList.add(new ServerGroup2Mgr());
        mgrList.add(new MammonConfigMgr());
		mgrList.add(new CrossServerGroupMgr());
		mgrList.add(new CrossMarriageGroupMgr());
        mgrList.add(new CrossUnionServerGroupMgr());
		mgrList.add(new AgentInterfaceMgr());
        mgrList.add(new SkinMgr());
        mgrList.add(new TempCommonMgr());
        mgrList.add(new TempGoodsBagMgr());
        mgrList.add(new VehicleMgr());
        mgrList.add(new PatronsMgr());
        mgrList.add(new CurioConfigMgr());
        mgrList.add(new BeautyMgr());
        mgrList.add(new DAServerListMgr());
        mgrList.add(new CrossDataAnalyticsMgr());
        mgrList.add(new KafKaHelperMgr());

        //分组Mgr
        mgrList.add(new CrossUserMgr());
        mgrList.add(new GroupChatMgr());
        mgrList.add(new CrossRankMgr());
        mgrList.add(new ActivityDisperseGoldMgr());
//        mgrList.add(new CrossBanquetConfigMgr());
//        mgrList.add(new CrossBanquetMgr());
//        mgrList.add(new CrossBanquetAutoMgr());
//        mgrList.add(new CrossPartyMgr());
//        mgrList.add(new CrossBanquetChatMgr());
//        mgrList.add(new CrossPartyChatMgr());
        mgrList.add(new ZsRankMgr());
        mgrList.add(new ZsConfigMgr());
        mgrList.add(new ZsMgr());
        mgrList.add(new ZsGroupChatMgr());
        mgrList.add(new SystemGroupChatMgr());
		mgrList.add(new CrossMatchConfigMgr());
		mgrList.add(new CrossMatchMgr());
		mgrList.add(new MatchServerGroupMgr());
		mgrList.add(new CrossGroupMarriageMgr());
		mgrList.add(new CrossMarriageGroupChatMgr());
        mgrList.add(new EscortServerGroupMgr());
        mgrList.add(new UnionHuaBenServerGroupMgr());
        mgrList.add(new BeautyEscortConfigMgr());
        mgrList.add(new CrossBeautyEscortMgr());
        mgrList.add(new CrossBeautyEscortRankMgr());
        mgrList.add(new CrossBeautyEscortCacheMgr());
        mgrList.add(new SystemCrossServerGroupMgr());
        mgrList.add(new CrossMammonHallMgr());
        mgrList.add(new MonopolyMarketConfigMgr());
        mgrList.add(new MonopolyMarketCrossGroupMgr());
        mgrList.add(new MonopolyMarketMgr());
        mgrList.add(new GrandChildExamConfigMgr());
        mgrList.add(new GrandChildRankMgr());
        mgrList.add(new GrandChildMgr());
        mgrList.add(new CrossWingRoomMgr());
        mgrList.add(new WorkShopMgr());
        mgrList.add(new CricketServerGroupMgr());
        mgrList.add(new CrossCurioMgr());

//        mgrList.add(new MogulBirthdayMgr());
		return mgrList;
	}
}
